#pragma once
#include<iostream>
#include<unistd.h>
using namespace std;

typedef void (*work_t)(int);//函数指针类型
typedef void(*task_t)();

void task1()
{
    cout<<"i'm task11111, hello people! from sub process: "<<getpid()<<endl;
}

void task2()
{
    cout<<"i'm task22222, hello people! from sub process: "<<getpid()<<endl;
}

void task3()
{
    cout<<"i'm task33333, hello people! from sub process: "<<getpid()<<endl;
}

//任务的函数指针数组，存储任务列表
task_t taskarray[]={task1,task2,task3};

//寻找下一个派发的任务
int NextTask()
{
    //随机抽取任务
    return rand()%3;
}

//工作过程
void worker(int rfd)
{
    while(true)
    {
        sleep(1);
         int taskcode=0;
        //接收任务码
        int n=read(rfd,&taskcode,sizeof(taskcode));
        //当可以读取到任务信息执行任务
        if(n==sizeof(taskcode))
        {
            //执行任务
            taskarray[taskcode]();
               cout<<"task success excute... processid: "<<getpid()<<endl<<endl;
        }
        else//读取不到任务信息即退出
        {
            cout<<"no task can excute,exit...  processid: "<<getpid()<<endl<<endl;
            break;
        }
    }
   

}

